
/*
 * PowerOut class
 * This class represents when power goes out. Extends from Event
 * 
 * James Zhang
 * ID: 3069886
 * July 17, 2012
 */
public class PowerOut extends Event
{
    /**
     * Default constructor
     *
     * @param g - Passed in the governing GreenhouseControls object reference
     * @param delayTime - Delay time for when to start this event
     */
    public PowerOut(GreenhouseControls g, long delayTime)
    {
        super(g, delayTime);
    }

    /**
     * Overridden run method which sets the power to false and throws a ControllerException
     *
     * @throws ControllerException
     */
    public synchronized @Override
    void run()
    {
        while (timePassed < delay)
        {
            try
            {
                thread.sleep(1000);
                timePassed += 1000;
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        gc.crashTime = this.delay;
        System.out.println(toString());
        gc.setVariable("poweron", false);
        try
        {
            errortime();
        } catch (ControllerException ex)
        {
        }
        this.setThread(true);
        gc.shutdown(2);
    }

    //Method which throws the exception
    public void errortime() throws ControllerException
    {
        throw new ControllerException( "Power Out!", 2 );
    }

    /**
     * Override of toString()
     *
     * @return - Returns event in sentence form
     */
    public String toString()
    {
        return "Problem occured!";
    }
}